USE [master];
GO

IF EXISTS (SELECT * FROM [sys].[databases] WHERE [name] = N'FilestreamWpfHttp')
BEGIN
	ALTER DATABASE FilestreamWpfHttp SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
	DROP DATABASE FilestreamWpfHttp;
END;
GO

DECLARE @root [nvarchar](512);
EXEC [sys].[xp_instance_regread] N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\Setup', N'SQLPath', @root OUTPUT;

DECLARE @defaultFile [nvarchar](512);
EXEC [sys].[xp_instance_regread] N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData', @defaultFile OUTPUT;
                  
DECLARE @defaultLog [nvarchar](512);
EXEC [sys].[xp_instance_regread] N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog', @defaultLog OUTPUT;

IF @defaultFile IS NULL
	SET @defaultFile = @root + N'\Data';

IF @defaultLog IS NULL
	SET @defaultLog = @root + N'\Data';
	
DECLARE @batch [nvarchar](max);

SET @batch =
	N'CREATE DATABASE FilestreamWpfHttp ON PRIMARY
	  ( NAME = FilestreamWpfHttp_data, 
		FILENAME = ''' + @defaultFile + N'\FilestreamWpfHttp_data.mdf''),
	FILEGROUP FilestreamGroup1 CONTAINS FILESTREAM
	  ( NAME = FilestreamWpfHttp_media, 
		FILENAME = ''' + @defaultFile + N'\FilestreamWpfHttp_media'')
	LOG ON 
	  ( NAME = FilestreamWpfHttp_log, 
		FILENAME = ''' + @defaultLog + N'\FilestreamWpfHttp_log.ldf'');'

EXECUTE ( @batch );
GO
